/*
* Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* $Id: ircmodelitem.cpp 324 2009-02-25 19:39:02Z jpnurmi $
*/

#include "ircmodelitem.h"
#include <ircsession.h>

Q_DECLARE_METATYPE(Irc::Session*);

IrcModelItem::IrcModelItem(Irc::Session* session) : QStandardItem()
{
	setEditable(false);
    setSession(session);
}

IrcModelItem::~IrcModelItem()
{
}

bool IrcModelItem::operator<(const QStandardItem& other) const
{
    QChar t = text().at(0);
    QChar o = other.text().at(0);
    if (t != o)
    {
        if (t == '@' || o == '@')
            return t == '@';
        else if (t == '+')
            return o != '@';
        else if (o == '+')
            return o == '@';
    }
    return QString::localeAwareCompare(text().toLower(), other.text().toLower()) < 0;
}

IrcModelItem* IrcModelItem::child(int index) const
{
    return static_cast<IrcModelItem*>(QStandardItem::child(index));
}

IrcModelItem* IrcModelItem::childItem(const QString& name)
{
    IrcModelItem* child = findChild(name);
    if (!child)
    {
        child = new IrcModelItem(session());
        appendRow(child);
        child->setData(name, Qt::DisplayRole);
    }
    return child;
}

IrcModelItem* IrcModelItem::findChild(const QString& name) const
{
    QStandardItem* item = 0;
    if (model())
    {
        QStandardItem* first = child(0);
        if (first)
        {
            QString nick = name;
            if (nick.startsWith("@") || nick.startsWith("+"))
                nick.remove(0, 1);
            QModelIndexList list = model()->match(first->index(), NameRole, nick, 1, Qt::MatchFixedString);
            item = model()->itemFromIndex(list.value(0));
        }
    }
    return static_cast<IrcModelItem*>(item);
}

QVariant IrcModelItem::data(int role) const
{
    if (role == Qt::DisplayRole || role == Qt::EditRole)
    {
        QString m = mode();
        if (m.contains("o"))
            m = "@";
        else if (m.contains("v"))
            m = "+";
        return m + name();
    }
    return QStandardItem::data(role);
}

void IrcModelItem::setData(const QVariant& value, int role)
{
    if (role == Qt::DisplayRole || role == Qt::EditRole)
    {
        QString str = value.toString();
        while (str.startsWith("@") || str.startsWith("+"))
        {
            if (str.startsWith("@"))
                setMode("+o");
            else
                setMode("+v");
            str.remove(0, 1);
        }
        setName(str);
    }
    else
    {
        QStandardItem::setData(value, role);
    }
}

QString IrcModelItem::mode() const
{
    return data(ModeRole).toString();
}

void IrcModelItem::setMode(const QString& mode)
{
    QString m = IrcModelItem::mode();
    if (mode.startsWith("+"))
        m.append(mode.mid(1));
    else if (mode.startsWith("-"))
        m.remove(mode.mid(1));
    setData(m, ModeRole);
}

QString IrcModelItem::name() const
{
    return data(NameRole).toString();
}

void IrcModelItem::setName(const QString& name)
{
    setData(name, NameRole);
}

Irc::Session* IrcModelItem::session() const
{
    return data(SessionRole).value<Irc::Session*>();
}

void IrcModelItem::setSession(Irc::Session* session)
{
    setData(QVariant::fromValue(session), SessionRole);
}
